/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.uhk.ppro.response.web;

import cz.uhk.ppro.response.model.Answer;
import cz.uhk.ppro.response.model.MobileQuestionsAnswers;
import cz.uhk.ppro.response.model.Question;
import cz.uhk.ppro.response.model.Test;
import cz.uhk.ppro.response.service.DatabaseService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Martin
 * Class vracejici mobilnimu zařízení Otázky a odpovědi v závislosti na id, které obdrží od zařízení
 */

@Controller
public class MobileQuestionsAnswersController {
    
    @Autowired
    private DatabaseService databaseSession;
    
    @RequestMapping(value="testmobileresponse", method = RequestMethod.POST)
	public @ResponseBody MobileQuestionsAnswers getQuAnInJSON(
                                        // @PathVariable String testId
                                        @RequestParam(value="idTest",required=false) String testId
                ) {
      
        MobileQuestionsAnswers mqa = new MobileQuestionsAnswers();
       
        List<Question> questions = databaseSession.getQuestionsByIdTest(Integer.parseInt(testId));
       
        for(Question q : questions){
            mqa.addQuestion(q.getId(), q.getName());

            List<Answer> answers = databaseSession.getAnswersByQuestionId(q.getId());
            for(Answer a : answers){
                 mqa.addAnswer(a.getId(), a.getName());
                mqa.addQuestionAnswer(a.getId(),q.getId());
            }        
            
        }
        
  
        return mqa;
	}
    
    
    
    
    
    
}
